//
//  MVVMDataSource.m
//  Demo_MVVM
//
//  Created by 陈冰 on 2020/3/8.
//  Copyright © 2020 ChenBing. All rights reserved.
//

#import "MVVMDataSource.h"

@interface MVVMDataSource ()

@property (nonatomic, copy) NSString *cellReuseIdentifier;
@property (nonatomic, copy) CellConfigBeforeBlock cellConfigBeforeBlock;
@property (nonatomic, copy) SelectCellBlock selectCellBlock;
@property (nonatomic, copy) ReloadDataBlock reloadDataBlock;

@end

@implementation MVVMDataSource

- (instancetype)initWithCellResueIdentifier:(NSString *)cellRI
                      cellConfigBeforeBlock:(CellConfigBeforeBlock)aCellConfigBeforeBlock
                            selectCellBlock:(SelectCellBlock)aSelectCellBlock
                            reloadDataBlock:(ReloadDataBlock)aReloadDataBlock {
    self = [super init];
    if (self) {
        _cellReuseIdentifier = cellRI;
        _cellConfigBeforeBlock = [aCellConfigBeforeBlock copy];
        _selectCellBlock = [aSelectCellBlock copy];
        _reloadDataBlock = [aReloadDataBlock copy];
    }
    return self;
}

- (void)addDataToArray:(NSArray *)aDatas {
    if (!aDatas) return;
    if (self.dataArray.count > 0) {
        [self.dataArray removeAllObjects];
    }
    [self.dataArray addObjectsFromArray:aDatas];
}

- (id)modelAtIndexPath:(NSIndexPath *)indexPath {
    return self.dataArray.count > indexPath.row ? self.dataArray[indexPath.row] : nil;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    // 将点击事件通过block的方式传递出去
    if (self.selectCellBlock) {
        self.selectCellBlock(indexPath);
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:self.cellReuseIdentifier forIndexPath:indexPath];
    id model = [self modelAtIndexPath:indexPath];
    if (self.cellConfigBeforeBlock) {
        self.cellConfigBeforeBlock(cell, model, indexPath);
    }
    return cell;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;
}

/*改变删除按钮的title*/
- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    return @"删除";
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle  forRowAtIndexPath:(NSIndexPath *)indexPath {
    if  (editingStyle == UITableViewCellEditingStyleDelete) {
        /*先删除数据，再执行UI*/
        // 一维数组删除某行
        [self.dataArray removeObjectAtIndex:indexPath.row];
        // 二维数组，删除某个section下的行
        // [[self.dataArray objectAtIndex:indexPath.section] removeObjectAtIndex:indexPath.row];
        /* 删除tableView中的一行，动画自动，单行刷新*/
        [tableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObject:indexPath]  withRowAnimation:UITableViewRowAnimationAutomatic];
        // 报错则用 [tableView reloadData];
        if (self.reloadDataBlock) {
            self.reloadDataBlock(self.dataArray);
        }
    }
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:self.cellReuseIdentifier forIndexPath:indexPath];
    id model = [self modelAtIndexPath:indexPath];
    if (self.cellConfigBeforeBlock) {
        self.cellConfigBeforeBlock(cell, model, indexPath);
    }
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    // 将点击事件通过block的方式传递出去
    if (self.selectCellBlock) {
        self.selectCellBlock(indexPath);
    }
}

- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray new];
    }
    return _dataArray;
}

@end
